package com.xzm.utils;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 瞎琢磨先生 https://github.com/xiazuomo/xzm-utils.git
 * @date 2022/7/29 23:51
 */
@Slf4j
public class ExcelUtils {

    private ExcelUtils() {
    }

    /**
     * 注意事项："请在实体类的成员变量上添加 @Alias 注解一一绑定 Excel 中的字段！"
     */
    public static <T> List<T> getExcelReader(MultipartFile file, Class<T> clazz, String sheetName) {
        ExcelReader excelReader = null;
        try (InputStream in = file.getInputStream()) {
            excelReader = ExcelUtil.getReader(in, sheetName);
        } catch (Exception e) {
            log.error("NewExcelUtils.getExcelReader 接口运行异常：", e);
        }
        if (excelReader == null) {
            return new ArrayList<>();
        }
        final List<T> excelList = excelReader.readAll(clazz);

        ObjectUtils.isAllFiledNull(excelList);

        return excelList;
    }


}
